/**
 * @(#)ExcelReader.java, 9月 18, 2023.
 * <p>
 * Copyright 2023 yuanfudao.com. All rights reserved.
 * FENBI.COM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package yfd;

import lombok.Data;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author leiwenkang
 */
public class ExcelReader2 {
    public static void main(String[] args) throws IOException {
        String res = readExcel("/Users/leiwenkang/Documents/project/personal/offer-study/src/main/resources/作文内测用户list.xlsx");
        System.out.println(res);
    }
    

    public static String readExcel(String excelFilePath) throws IOException {

        FileInputStream fis = new FileInputStream(excelFilePath);

        // create a new workbook from the input stream
        Workbook workbook = new XSSFWorkbook(fis);
        // get the first sheet
        Sheet sheet = workbook.getSheetAt(0);

        int firstRow = sheet.getFirstRowNum();
        int lastRow = sheet.getLastRowNum();

        DecimalFormat df = new DecimalFormat("0");
        StringBuilder sb = new StringBuilder();
        for (int i = firstRow; i <= lastRow; i++) {
            Row row = sheet.getRow(i);
            // For each row, iterate through all the columns
            Cell cell = row.getCell(0);
            sb.append(df.format(cell.getNumericCellValue()));
            sb.append(",");
        }
        workbook.close();
        fis.close();

        return sb.substring(0, sb.length() - 1);
    }
}